package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVO;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000147695412";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCXxnOJa/jHw7QMWic962WQ6pyPE0Qm8+4/L1BlXATSWSCDv5LeaMLnVEueSO7dbQoIgY9XU6EZvmEZmWTgcqfkyHEid6aZijMSjngEG0noHsQ+DKQAm+fzI4doc3KiE9nvObi/zsomthMlwrMOCwBVn4mb1bGHt27tIVL1nwyna7zAQYwQclyxganwuA1ZT6EBce1g+ySqgGsyPMMzdHnonoKAMZO3238mcriKsOHU3IjTcxm4FYaZ6sRTzS1x6N7xuWImlbIum0VXvSimsxFULaWx3qSfaNURaUmvmEgalt/naIgn6/jTL895r/QjDUBy1mtc4J5VvN/tapI5oISVAgMBAAECggEAH1rFFZezrnk0ofSh9L3DnruJGlmtk6qDBpSctEPdTyNQf0c9bSsRUL7uXEXO6vmSwA0b8rt5JZtZ7fLtjxzAqJfsH2W+iiutv2ClVJvamdR5UCRGYUXUFju5xo9LGEanmC9BhoaO5o5oyD0/xl/pcsbho8rywQnZJa3juJBiCdf7ACEIwOVMD0c7RDmIvaZMveMrvW08NYCiKVdsYlAjyv72w4qeR2YNpIpA3yxtqBQgpIb6/rvjUivNqUloU4/nBMqL2tZyg0/vGT0vjE+UImGMI98twjgV7nwGry5VxsFCTKDv9JsbeHKWatX/nZlg+n9plKpuOBiFP5MOE40NRQKBgQDeMrKENh2y4tJcm4yi7o70qLkPoUPJ7yvwlX79PMWttiMPbo/ZueGVDulf0MAKRGgSjQWshobbsDLvZoew45s9PVhxIySzULAFGaDA/ztgZtpVDPoMK08fepZTTwJJhTpMWO3tG7/3LeMx5VYpBTOjkoKkyEE5mxnjer8QzrLP7wKBgQCu3TMx8bNRvYeHDR0aTIR2MIZP9n6BAdeYgb9/x+THaStz7dmVzRxdWJByxiGIw34cS/5eNGVQZcrRopUKjt29cu0dnGd1ikdnBTXcC2VNxcGdzRYHZm7Blc3HQPiG0Pqce8FUqRJ1tqfyBCEFpUC1hCRGxxHiuWmcmYotPsZvuwKBgQDQl59m5rayvKTnM0+vlWy22b4Bok7VP6ZsUKvKaEbbcOUxE4YPy6SuHjWQMZRXDyj0YVc2Q4ROsPbgL8yqjsGpl/FzJJ7Lh6/22bxD8ld3hJbqySfkTLQWxSpiQwXOpze3fLS/pxFrprRmnvlnwO5dXKkwsJ4qyiRfnVfJG4JaGQKBgQCOD8E20zEmuD6x1lKNehqnVnbX4d8WeeOoz8g/TKut4eBed56s2A0yPPBlflfiH0iGPqrx58ljjr8+nsJM6JJ3BjZjNxzKXkPyC0addVig3YeV5vb9nhdfCJA8T6lZBdCEbvo7CHypy3IX6YjWaFHw5eD+6125f36wrgCXD4fxXwKBgFwoKXA3Tkjmqwr4kZ6qkNe3dtX5gCwG/DeECCIcVZn2eVpdihOg7yLgkqQkzfCBsW1wRf3WSbT3O/Qaby/qqDEW6DMgDss3ME/M5uXg9X86QP4A1WLlmfppdEXHhxg9ZDLsQmpo156g1qHbnAIE8qYnf77qv6ju/Gmi8NXPwKyo";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArSMDoPQ+tbRyf7SXCDqtBwniWUdyxwCgVZGfN0AyIuTR+EEFEdqmvzBIvpEvbgtzxjk9XQBWCPbFsp/0D7IOaLNafEjlb33+M0DrT34EiYbvkLqX4e/qte+ZTItCmZfnUGEiL3M22heFXhbIsk2adpK8KYtt47781A1N5sAQKj2Kog+nHZUaIUPqoDYaglFiYJy26k47iMoXe/l7iOflQyEVP8GNToblp63l7uB1aupEkgZfVj+Pp0Izy+lhgoT1y3mCc8oz0NF3Bv4KgY3LjaQDxUwYTVvqmPnlPui4TYBGLTNEPqI+5KatWP1NGd4v/y+wfAsNKzZC4DUAS96pSQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://6481d74ba0e47c54.natapp.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVO vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
